package com.koo.framework.handler;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.koo.common.utils.UserUtils;
import com.koo.common.vo.UserVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.util.Date;

@Slf4j
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {

    @Override
    public void insertFill(MetaObject metaObject) {
        UserVo user = UserUtils.get();
        if (metaObject.hasSetter("createTime") && this.getFieldValByName("createTime", metaObject) == null) {
            this.setFieldValByName("createTime", new Date(), metaObject);
        }
        if (metaObject.hasSetter("updateTime") && this.getFieldValByName("updateTime", metaObject) == null) {
            this.setFieldValByName("updateTime", new Date(), metaObject);
        }
        if (metaObject.hasSetter("status") && this.getFieldValByName("status", metaObject) == null) {
            this.setFieldValByName("status", 1, metaObject);
        }
        if (metaObject.hasSetter("isDeleted") && this.getFieldValByName("isDeleted", metaObject) == null) {
            this.setFieldValByName("isDeleted", 0, metaObject);
        }
        if(user != null){
            if (metaObject.hasSetter("createUsername") && this.getFieldValByName("createUsername", metaObject) == null) {
                this.setFieldValByName("createUsername", user.getUsername(), metaObject);
            }
            if (metaObject.hasSetter("createUserId") && this.getFieldValByName("createUserId", metaObject) == null) {
                this.setFieldValByName("createUserId", user.getUserId(), metaObject);
            }
            if (metaObject.hasSetter("updateUsername") && this.getFieldValByName("updateUsername", metaObject) == null) {
                this.setFieldValByName("updateUsername", user.getUsername(), metaObject);
            }
            if (metaObject.hasSetter("updateUserId") && this.getFieldValByName("updateUserId", metaObject) == null) {
                this.setFieldValByName("updateUserId", user.getUserId(), metaObject);
            }
            if (metaObject.hasSetter("storeId") && this.getFieldValByName("storeId", metaObject) == null) {
                this.setFieldValByName("storeId", user.getStoreId(), metaObject);
            }
            if (metaObject.hasSetter("storeName") && this.getFieldValByName("storeName", metaObject) == null) {
                this.setFieldValByName("storeName", user.getStoreName(), metaObject);
            }
            if (metaObject.hasSetter("storeCode") && this.getFieldValByName("storeCode", metaObject) == null) {
                this.setFieldValByName("storeCode", user.getStoreName(), metaObject);
            }
        }

    }

    @Override
    public void updateFill(MetaObject metaObject) {
        UserVo user = UserUtils.get();
        if (metaObject.hasSetter("updateTime") && this.getFieldValByName("updateTime", metaObject) == null) {
//            this.strictUpdateFill(metaObject, "updateTime", LocalDateTime.class, LocalDateTime.now());
            this.setFieldValByName("updateTime", new Date(), metaObject);
        }

        if(user != null){
            if (metaObject.hasSetter("updateUsername") && this.getFieldValByName("updateUsername", metaObject) == null) {
//                this.strictUpdateFill(metaObject, "updateUsername", String.class, user.getUsername());
                this.setFieldValByName("updateUsername", user.getUsername(), metaObject);
            }
            if (metaObject.hasSetter("updateUserId") && this.getFieldValByName("updateUserId", metaObject) == null) {
//                this.strictUpdateFill(metaObject, "updateUserId", String.class, user.getUserId() + "");
                this.setFieldValByName("updateUserId", user.getUserId(), metaObject);
            }
        }

    }

}
